<?php


namespace app\manager\controller;


use app\common\controller\Common;
use app\common\model\Management;
use think\captcha\Captcha;
use think\facade\Session;
class Login extends Common
{
    public function login(){
        if (request()->isPost()) {
            $name = request()->param('username');
            $pass = request()->param('password');
            $captcha = request()->param('captcha');
            $cap=new Captcha();
            if($cap->check($captcha)){
                $retManager = Management::where('username','=',$name)->field('password,manid')->find();
                if (empty($retManager)) return $this->errJson('登录失败！');
                if(password_verify($pass,$retManager->password)){
                    $retManager->update_time = date("Y-m-d H:i:s", time());
                    $retManager->save();
                    unset($manger);
                    Session::set('manager',$name);
                    return $this->okJson('登录成功！',['url'=>'/manager/index/index']);
                }else{
                    return $this->errJson('登录失败！');
                }
            }else{
                return $this->errJson('验证码错误');
            }

        }else{
            return view();
        }

    }

    public function mod()
    {
            $username = request()->param('username');
            $password = request()->param('password');
            $manid = request()->param('manid');
            if (isset($manid) && $manid!= "") {
                $manger = Management::get($manid);
                if ($manger != NULL) {
                    if (isset($username)) {
                        $manger->username = trim($username);
                    }
                    if (isset($password) && $password != "") {
                        $pass = trim($password);
                        $sa = substr($pass, 2, 1);
                        $repass = $this->samd5($sa, $pass);
                        $manger->password = $repass;
                    }
                    $result = $manger->save();
                    if ($result) {
                        return $this->okJson('修改成功');
                    } else {
                        return $this->errJson('参数不能为空');
                    }
                }
            } else {
                return $this->errJson('参数不能为空');
            }

    }


    function loginout(){
        Session::delete('manager');
        $this->error('已退出！',url('/manager/login/login'));
    }
}